/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

//import gjt.StopwatchClient;

import gjt.Assert;

import java.io.Serializable;

/**
 * A Thread that acts as a stopwatch.
 *
 */
public class Stopwatch extends Thread implements Serializable {
    static private long _defaultTickInterval = 50;

    private StopwatchClient client;
    private long start, now, elapsed;
    private long hour, minute, second, millisecond;
    private long tickInterval;

    public Stopwatch() {
        this(null);
    }

    public Stopwatch(StopwatchClient client) {
        this(client, _defaultTickInterval);
    }

    public Stopwatch(StopwatchClient client, long milliseconds) {
        tickInterval = milliseconds;
        start = System.currentTimeMillis();
        this.client = client;
    }

    public void setTickInterval(long milliseconds) {
        tickInterval = milliseconds;
    }

    public void update() {
        now = System.currentTimeMillis();
        elapsed = now - start;
        hour = minute = second = millisecond = 0;

        second = elapsed / 1000;
        millisecond = elapsed % 1000;
        millisecond = (millisecond == 0) ? 0 : millisecond / 10;

        if (second > 59) {
            minute = second / 60;
            second = second - (minute * 60);
        }
        if (minute > 59) {
            hour = minute / 60;
            minute = minute - (hour * 60);
        }
    }

    public String toString() {
        update();
        return new String(stringValueOf(hour) + ":" +
                stringValueOf(minute) + ":" +
                stringValueOf(second) + ":" +
                stringValueOf(millisecond));
    }

    public long getHour() {
        return hour;
    }

    public long getMinute() {
        return minute;
    }

    public long getSecond() {
        return second;
    }

    public long getMillisecond() {
        return millisecond;
    }

    public long elapsedTime() {
        update();
        return elapsed;
    }

    public void reset() {
        start = System.currentTimeMillis();
    }

    public void run() {
        while (true) {
            try {
                Thread.currentThread().sleep(tickInterval, 0);
                update();
                if (client != null)
                    client.tick(this);
            }
            catch (InterruptedException e) {
                Assert.notFalse(false);
            }
        }
    }

    private String stringValueOf(long l) {
        if (l < 10) return "0" + String.valueOf(l);
        else return String.valueOf(l);

    }
}
